import 'package:flutterstudyapplast/business/models/user_profile.dart';
import 'package:flutter/foundation.dart';
import 'package:flutterstudyapplast/services/base_viewmodel.dart';
import 'package:flutterstudyapplast/services/service_locator.dart';
import 'package:flutterstudyapplast/services/user/user_service.dart';

class UserViewModel extends BaseViewModel with ChangeNotifier{
  final UserService _service = serviceLocator<UserService>();

  UserProfile _profile = UserProfile();


  Future<bool> login(String username,String password) async{
    var result = await _service.login(username, password);
    if(result["errorCode"] == "-1"){
      return false;
    }else{
      var data = result["data"];
      _profile.name = data["nickname"];
      _profile.userId = data["id"];
    }
    notifyListeners();
    return true;
  }
  Future<bool> register(String username,String password) async {
    var result = await _service.register(username, password);
    if(result["errorCode"] == "-1"){
      return false;
    }else{
      var data = result["data"];
      _profile.name = data["nickname"];
      _profile.userId = data["id"];
    }
    notifyListeners();
    return true;
  }
}